﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;
using System.Data.Common;

namespace CodeSmith.SchemaHelper.Util
{
    public class InfoMessageArgs : EventArgs
    {
        public InfoMessageArgs(Exception exception):base()
        {
            InnerException = exception;
        }
        public Exception InnerException { get; private set; }
    }
    public sealed class OracleScriptUtility
    {
        public string ConnectionString { set; get; }
        public string Script { set; get; }
        public event EventHandler<InfoMessageArgs> ScriptInfoMessage;
        public OracleScriptUtility() { }
        public OracleScriptUtility(string connectionString, string script) 
        {
            ConnectionString = connectionString;
            Script = script;
        }

        public OracleScriptResult ExecuteScript()
        {
            return ExecuteScript(ConnectionString, Script, ScriptInfoMessage);
        }

        public static OracleScriptResult ExecuteScript(string connectionString, string script)
        {
            return ExecuteScript(connectionString, script, null);
        }

        public static OracleScriptResult ExecuteScript(string connectionString, string script, EventHandler<InfoMessageArgs> infoMessageHandler)
        {
           
            OracleScriptResult result = null;
            DbConnection conn = null;
            try
            {
                conn = DBHelper.GetConnection(connectionString);
            }
            catch (Exception ex)
            {
                result = new OracleScriptResult(false, ex.Message );
                if (conn != null)
                {
                    conn.Dispose();
                }
                return result;
            }

            

            DbCommand command = conn.CreateCommand();
            command.CommandText = script;
            command.CommandType = System.Data.CommandType.Text;
            try
            {
                conn.Open();
                command.ExecuteNonQuery();
                result = new OracleScriptResult(true, null);
            }
            catch (Exception ex)
            {
                result = new OracleScriptResult(false, ex.Message);
                if (infoMessageHandler != null)
                {
                    infoMessageHandler(null, new InfoMessageArgs(ex));
                }
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        public static OracleScriptResult ExecuteScript(string connectionString, string[] scripts, EventHandler<InfoMessageArgs> infoMessageHandler)
        {           
            OracleScriptResult result = null;
            DbConnection conn = null;
            try
            {
                conn = DBHelper.GetConnection(connectionString);
            }
            catch (Exception ex)
            {
                result = new OracleScriptResult(false, ex.Message);
                if (conn != null)
                {
                    conn.Dispose();
                }
                return result;
            }


            DbCommand command = conn.CreateCommand();            
            command.CommandType = System.Data.CommandType.Text;
            
            try
            {
                conn.Open();
                foreach (string script in scripts)
                {
                    if (script.Trim() == "")
                    {
                        continue;
                    }
                    command.CommandText = script;                    
                    command.ExecuteNonQuery();
                }
                result = new OracleScriptResult(true, null);
            }
            catch (Exception ex)
            {
                result = new OracleScriptResult(false, ex.Message);
                if (infoMessageHandler != null)
                {
                    infoMessageHandler(null,new InfoMessageArgs(ex));
                }
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
  
    }

}
